package org.redis;

import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisClientConfig;
import java.net.URI;
import java.util.List;
import java.util.Set;

public class RedisTool {

    private static  Jedis jedis ;

    public Jedis getConnection() throws Exception {
        if(jedis!=null){
            return jedis;
        }
        JedisClientConfig clientConfig = DefaultJedisClientConfig.builder().clientName("rayman")
                .blockingSocketTimeoutMillis(1000)
                .connectionTimeoutMillis(1000)
                .socketTimeoutMillis(1000)
                .ssl(false)
                .database(0)
                .build();
        return new Jedis(new URI("redis://localhost:6379/0"),clientConfig);
    }


    public void setString(String key,String value){
        jedis.set(key,value);
    }

    public void setHash(String key ,String field,String value){
        jedis.hset(key.getBytes(),field.getBytes(),value.getBytes());
    }

    public void setListL(String key ,String value){
        jedis.lpush(key,value);
    }

    public void setListR(String key ,String value){
        jedis.rpush(key,value);
    }

    public String popFromListL(String key ){
        return jedis.lpop(key);
    }

    public String popFromListR(String key ){
        return jedis.rpop(key);
    }

    public void setListIndex(String key , String value,int index ){
        jedis.lset(key , index,value);
    }

    public List<String> rangeList(String key , int start, int end ){
        return jedis.lrange(key , start,end);
    }

    public void setSet(String key ,String value){
        jedis.sadd(key,value);
    }

    public String popSet(String key){
        return jedis.spop(key);
    }

    public Set<String> popSet(String key, long count){
        return jedis.spop(key,count);
    }

    public Long getSetNumber(String key){
        return  jedis.scard(key);
    }

    public Set<String> getSinSets(String[] keys){
        return  jedis.sinter(keys);
    }

    public long setNx(String key ,String value){
        return jedis.setnx(key,value);
    }

    public long expire(String key ,long ttl){
        return jedis.expire(key,ttl);
    }

    public String lpop(String key){
        return jedis.lpop(key);
    }
}



//set hset hmset lpush rpush lset
//del unlink
//get hget hmget lpop rpop